package customer.zspt.cn.customer.Widget;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.utils.MediaPlayUtils;
import customer.zspt.cn.customer.utils.NetUtils;
import customer.zspt.cn.customer.utils.TimestampToBj;
/**
 * Created by Administrator on 2018/8/8 0008.
 */
public class ChatVocieLeftMsgItem extends LinearLayout implements View.OnClickListener{
    /**播放音频*/
    private final LinearLayout playVoice;
    private final ImageView play;
    private final AnimationDrawable animationDrawable;
    private final TextView httpAudio;
    private final ImageView header_portrait;
    private final TextView msg_time;
    private final TextView voice_time;
    private final BitmapUtils utils;
    //是否在播放音频
    public boolean IsPlay = false;
    public ChatVocieLeftMsgItem(Context context) {
        this(context,null);
    }
    public ChatVocieLeftMsgItem(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }
    public ChatVocieLeftMsgItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        utils = new BitmapUtils(context);
        View.inflate(context, R.layout.voicefrommsg,this);
        playVoice = (LinearLayout)findViewById(R.id.playVoice);
        playVoice.setOnClickListener(this);
        play = (ImageView)findViewById(R.id.play);
        header_portrait = (ImageView)findViewById(R.id.header_portrait);
        msg_time = (TextView)findViewById(R.id.msg_time);
        voice_time = (TextView) findViewById(R.id.voice_time);
        animationDrawable = (AnimationDrawable) play.getBackground();
        httpAudio = (TextView) findViewById(R.id.httpAudio);
    }
    @Override
    public void onClick(View v) {
         switch (v.getId()){
             case R.id.playVoice:
                 if(NetUtils.getNetWorkState(getContext())==NetUtils.NOTNET){
                     Toast.makeText(getContext(),"无网络",Toast.LENGTH_SHORT).show();
                 }else{
                     if(IsPlay){
                         IsPlay = false;
                         MediaPlayUtils.getInstance().stop();
                     }else{
                         IsPlay = true;
                         MediaPlayUtils.getInstance().setPreparedListener(this);
                         MediaPlayUtils.getInstance().playUrl(httpAudio.getText().toString());
                     }
                 }
                 break;
         }
    }
    /**
     * 是否播放动画
     */
    public void IsPlayAnimation(boolean isPlay) {
        if(isPlay){
            if(!animationDrawable.isRunning()){
                animationDrawable.start();
            }
        }else{
            if(animationDrawable.isRunning()){
                animationDrawable.stop();
            }
        }
    }
    /**设置网站*/
    public void setHttpAudio(String url){
        httpAudio.setText(url);
    }
    /**设置头像*/
    public void setAvater(String url){
            utils.display(header_portrait,url);
    }
    /**音频时间*/
    public void audioTime(String time){
        voice_time.setText(zhTime(Integer.parseInt(time)));
    }
    public String zhTime(long time){
        String timeStr = "";
        long m = time/1000/60;
        long s = time/1000%60;
        if(m==0){
            timeStr = s+"''";
        }else{
            timeStr = m+"'"+s+"''";
        }
        return timeStr;
    }

    /**设置时间*/
    public void setTime(long time,long ChatTime)
    {
        if (time - ChatTime > 3 * 60 * 1000) {
            msg_time.setText(TimestampToBj.getBeijingTime(time));
        } else {
            msg_time.setVisibility(View.GONE);
        }
    }

}
